package com.daguchuangyi.erp_android.production_detail;

import android.app.Activity;
import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.daguchuangyi.erp_android.R;
import com.daguchuangyi.erp_android.base.TransStatusBarActivity;
import com.daguchuangyi.erp_android.production_detail.widget.PinchImageView;
import com.jude.utils.JUtils;

import static com.raizlabs.android.dbflow.config.FlowManager.getContext;

public class BigPictureActivity extends TransStatusBarActivity {
    private static final String TAG_URL = "TAG_URL";
    private static final String TAG_FILE_NAME = "TAG_FILE_NAME";

    private String mUrl;
    private String mFileNameStr;

    private PinchImageView mPinchIv;
    private TextView mDownloadTv;

    public static void start(Context context, String url, String fileName) {
        Intent intent = new Intent(context, BigPictureActivity.class);
        if (!(context instanceof Activity)) {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        intent.putExtra(TAG_URL, url);
        intent.putExtra(TAG_FILE_NAME, fileName);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_big_picture);
        setStatusBarFontIconDark(false);

        mUrl = getIntent().getStringExtra(TAG_URL);
        mFileNameStr = getIntent().getStringExtra(TAG_FILE_NAME);

        mPinchIv = (PinchImageView) findViewById(R.id.pinch_iv);
        mDownloadTv = (TextView) findViewById(R.id.download_tv);
        RequestOptions requestOptions = new RequestOptions()
                .placeholder(R.color.normalGrey)
                .error(R.color.normalGrey)
                .diskCacheStrategy(DiskCacheStrategy.ALL);

        Glide.with(this).load(mUrl)
                .apply(requestOptions)
                .into(mPinchIv);

        mDownloadTv.setOnClickListener(view -> {
            if (TextUtils.isEmpty(mUrl)) {
                JUtils.Toast("图片为空，无法下载");
                return;
            }
            try {
                DownloadManager dm = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);
                Uri downloadUri = Uri.parse(mUrl);
                DownloadManager.Request request = new DownloadManager.Request(downloadUri);
                request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
                        .setAllowedOverRoaming(false)
                        .setTitle(mFileNameStr)
                        .setMimeType("image/jpeg")
                        .setDestinationInExternalPublicDir("/Download/", mFileNameStr)
                        .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                dm.enqueue(request);
                JUtils.ToastLong("图片正在下载到手机Download文件夹中,请在通知栏查看下载进度");
            } catch (Exception e) {
                JUtils.ToastLong("下载失败，请检查图片地址：" + mUrl);
                e.printStackTrace();
            }
        });
    }
}
